<?php
/* 
 * Author: Quan Van Sinh
 * Email: sinhvnb@vietnambiz.com
 */

// Function to remove HTML tags, Javascript,...
function filter($str) {
    $str = trim($str);
    $str = mysql_real_escape_string($str);
    $search = array ("'<'", // Strip out javascript
        "'>'",
        "'[\/\!]*?[^<>]*?>'si", // Strip out html tags
        "'([\r\n])[\s]+'", // Strip out white space
        "'&(quote|#34);'i", // Replace html entities
        "'&(amp|#38);'i",
        "'&(lt|#60);'i",
        "'&(gt|#62);'i",
        "'&(nbsp|#160);'i",
        "'&(iexcl|#161);'i",
        "'&(cent|#162);'i",
        "'&(pound|#163);'i",
        "'&(copy|#169);'i",
        "'&#(\d+);'e"); // evaluate as php
    $replace = array ("",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "");
    $text = preg_replace ($search, $replace, $str);
    $text = ereg_replace("'", "", $text);
    // $text=ereg_replace("\\", "", $text);
    return $text;
}

function CleanFileName( $Raw ) {
    $Raw = trim($Raw);
    $Raw = nosign($Raw);
    $RemoveChars  = array( "([\40])" , "([^a-zA-Z0-9-])", "(-{2,})" );
    $ReplaceWith = array("-", "", "-");
    return preg_replace($RemoveChars, $ReplaceWith, $Raw);
}

function checkLoginHeader() {
    global $DB; global $config;
    $rsUser = $DB->query_first("SELECT username, password FROM user WHERE username='".filter($_SESSION['username'])."' AND password='".filter($_SESSION['password'])."' AND active=1");
    if(!$rsUser) {
        $location = $config['domain'].'/'.$config['admin_section'].'/login';
        header("Location: $location");
    }

}

function cutString($str, $len) {
    $last_space = mb_strrpos($temp,' ');
    if(mb_strlen($str) > $len) {
        $temp = mb_substr($str, 0, $len);
        $last_space = mb_strrpos($temp,' ');
        $temp = mb_substr($temp, 0, $last_space).' ...';
        return $temp;
    } else {
        return $str;
    }
}

function getPathWay() {
    global $DB,$config;
    $sqlCat = "SELECT id,parent_id,name,link FROM article_category WHERE link='".$config['link']."'";
    $rsCat = $DB->query_first($sqlCat);
    if($_SESSION['clientActiveMenu']!='tag') {
        if($rsCat['parent_id']==0) {
            $motName = $rsCat['name'];
            if($_SESSION['clientActiveMenu']=='cata' || $_SESSION['clientActiveMenu']=='detail') {
                $_SESSION['pathWay'] = $motLink = $rsCat['link'];
                $strPath = '» <span>'.$motName.'</span>';
            }elseif($_SESSION['clientActiveMenu']=='archives') {
                $strPath = '» <span id="luuTruPath">Lưu trữ</span>';
            }elseif($_SESSION['clientActiveMenu']=='search') {
                $strPath = '» <span>Kết quả tìm kiếm</span>';
            }
        }else {
            $haiName = $rsCat['name'];
            $sqlMot = "SELECT id,parent_id,name,link FROM article_category WHERE id=".$rsCat['parent_id'];
            $rsMot = $DB->query_first($sqlMot);
            $motName = $rsMot['name'];
            if($_SESSION['clientActiveMenu']=='cata' || $_SESSION['clientActiveMenu']=='detail') {
                $_SESSION['pathWay'] = $motLink = $rsMot['link'];

                $sqlCatDetail = "SELECT id,name,link FROM article_category WHERE id=".$rsMot['parent_id'];
                $rsCatDetail = $DB->query_first($sqlCatDetail);
                if($rsCatDetail['name']!='') {
                    $_SESSION['pathWay'] = $rsCatDetail['link'];
                    $strPath = '» <a href="/cata/'.$rsCatDetail['link'].'" title="'.$rsCatDetail['name'].'">'.$rsCatDetail['name'].'</a>'.'» <span>'.$motName.'</span>';
                }else {
                    $strPath = '» <a href="/cata/'.$motLink.'" title="'.$motName.'">'.$motName.'</a>';
                    if($haiName != '') {
                        $strPath .= '» <span>'.$haiName.'</span>';
                    }
                }
            }else {
                $strPath = '» <a href="/cata/'.$motLink.'" title="'.$motName.'">'.$motName.'</a>';
                if($haiName != '') {
                    $strPath .= '» <span>'.$haiName.'</span>';
                }
            }

        }
    }else {
        $strPath .= '» <span id="tagPath">Từ khóa</span> » '.$config['link'];
    }

    return $strPath;
}

function getCategoryByParentId($parent_id=0) {
    global $DB;
    $sql= "SELECT * FROM article_category WHERE parent_id=$parent_id ORDER BY article_category.order asc";
    $sqlCount= "SELECT count(id) as count FROM article_category WHERE parent_id=$parent_id";
    $rs = $DB->query($sql);
    $rsCount = $DB->query_first($sqlCount);
    if($rsCount['count']) {
        return $rs;
    }else {
        return false;
    }
}

function cut_html_string($string, $limit) {
    include_once 'cutstring.php';
    $output = new HtmlCutString($string, $limit);
    return $output->cut();
}

function nosign($str) {
    $sign = array("à","á","ạ","ả","ã","â","ầ","ấ","ậ","ẩ","ẫ","ă",
        "ằ","ắ","ặ","ẳ","ẵ",
        "è","é","ẹ","ẻ","ẽ","ê","ề" ,"ế","ệ","ể","ễ",
        "ì","í","ị","ỉ","ĩ",
        "ò","ó","ọ","ỏ","õ","ô","ồ","ố","ộ","ổ","ỗ","ơ"
        ,"ờ","ớ","ợ","ở","ỡ",
        "ù","ú","ụ","ủ","ũ","ư","ừ","ứ","ự","ử","ữ",
        "ỳ","ý","ỵ","ỷ","ỹ","đ",
        "À","Á","Ạ","Ả","Ã","Â","Ầ","Ấ","Ậ","Ẩ","Ẫ","Ă"
        ,"Ằ","Ắ","Ặ","Ẳ","Ẵ",
        "È","É","Ẹ","Ẻ","Ẽ","Ê","Ề","Ế","Ệ","Ể","Ễ",
        "Ì","Í","Ị","Ỉ","Ĩ",
        "Ò","Ó","Ọ","Ỏ","Õ","Ô","Ồ","Ố","Ộ","Ổ","Ỗ","Ơ"
        ,"Ờ","Ớ","Ợ","Ở","Ỡ",
        "Ù","Ú","Ụ","Ủ","Ũ","Ư","Ừ","Ứ","Ự","Ử","Ữ",
        "Ỳ","Ý","Ỵ","Ỷ","Ỹ",
        "Đ","ê","ù","à"," ","_");

    $unsign = array("a","a","a","a","a","a","a","a","a","a","a"
        ,"a","a","a","a","a","a",
        "e","e","e","e","e","e","e","e","e","e","e",
        "i","i","i","i","i",
        "o","o","o","o","o","o","o","o","o","o","o","o"
        ,"o","o","o","o","o",
        "u","u","u","u","u","u","u","u","u","u","u",
        "y","y","y","y","y",
        "d",
        "A","A","A","A","A","A","A","A","A","A","A","A"
        ,"A","A","A","A","A",
        "E","E","E","E","E","E","E","E","E","E","E",
        "I","I","I","I","I",
        "O","O","O","O","O","O","O","O","O","O","O","O"
        ,"O","O","O","O","O",
        "U","U","U","U","U","U","U","U","U","U","U",
        "Y","Y","Y","Y","Y",
        "D","e","u","a","-","-");
    return strtolower(str_replace($sign, $unsign, $str));
}
function nosignTitle($str) {
    $sign = array("à","á","ạ","ả","ã","â","ầ","ấ","ậ","ẩ","ẫ","ă",
        "ằ","ắ","ặ","ẳ","ẵ",
        "è","é","ẹ","ẻ","ẽ","ê","ề" ,"ế","ệ","ể","ễ",
        "ì","í","ị","ỉ","ĩ",
        "ò","ó","ọ","ỏ","õ","ô","ồ","ố","ộ","ổ","ỗ","ơ"
        ,"ờ","ớ","ợ","ở","ỡ",
        "ù","ú","ụ","ủ","ũ","ư","ừ","ứ","ự","ử","ữ",
        "ỳ","ý","ỵ","ỷ","ỹ","đ",
        "À","Á","Ạ","Ả","Ã","Â","Ầ","Ấ","Ậ","Ẩ","Ẫ","Ă"
        ,"Ằ","Ắ","Ặ","Ẳ","Ẵ",
        "È","É","Ẹ","Ẻ","Ẽ","Ê","Ề","Ế","Ệ","Ể","Ễ",
        "Ì","Í","Ị","Ỉ","Ĩ",
        "Ò","Ó","Ọ","Ỏ","Õ","Ô","Ồ","Ố","Ộ","Ổ","Ỗ","Ơ"
        ,"Ờ","Ớ","Ợ","Ở","Ỡ",
        "Ù","Ú","Ụ","Ủ","Ũ","Ư","Ừ","Ứ","Ự","Ử","Ữ",
        "Ỳ","Ý","Ỵ","Ỷ","Ỹ",
        "Đ","ê","ù","à");

    $unsign = array("a","a","a","a","a","a","a","a","a","a","a"
        ,"a","a","a","a","a","a",
        "e","e","e","e","e","e","e","e","e","e","e",
        "i","i","i","i","i",
        "o","o","o","o","o","o","o","o","o","o","o","o"
        ,"o","o","o","o","o",
        "u","u","u","u","u","u","u","u","u","u","u",
        "y","y","y","y","y",
        "d",
        "A","A","A","A","A","A","A","A","A","A","A","A"
        ,"A","A","A","A","A",
        "E","E","E","E","E","E","E","E","E","E","E",
        "I","I","I","I","I",
        "O","O","O","O","O","O","O","O","O","O","O","O"
        ,"O","O","O","O","O",
        "U","U","U","U","U","U","U","U","U","U","U",
        "Y","Y","Y","Y","Y",
        "D","e","u","a");
    return str_replace($sign, $unsign, $str);
}
function replaceTitle($buffer) {
    global $config;
    $buffer = str_replace("[page_title]", $config ['site_title'],$buffer);
    $buffer = str_replace("[meta_description]", $config ['site_description'],$buffer);
    $buffer = str_replace("[meta_keyword]", $config ['site_keyword'],$buffer);
    return $buffer;
}
function paging($page,$total,$link='') {
    global $params;  global $config;
    if ($total>1) {
        if ($page!=1) {
            $dau=$page-3;
            if ($dau<=0) {$dau=1;$dit=3-$page;}
            $dit=$dit+$page+3;
            if ($dit>$total) {
                $dit=$total;$dau=$total-6;
                if ($dau<=0) {$dau=1;}
            }
        }
        else {
            $dau=1;$dit=6;
            if ($dit>$total) {$dit=$total;}
        }
        if ($page >$total) {$dau=1;$dit=1;}

        switch ($_SESSION['clientActiveMenu']) {
            case 'home':
                for ($i=$dau;$i<=$dit;$i++) {
                    if ($i==$page) {
                        $pages.='<li><a href="javascript: void(0);" class="currentpage">'.$i.'</a></li>';
                    }else {
                        $pages.='<li><a href="/newarticle_'.$i.'">'.$i.'</a></li>';
                    }
                }
                if($page==1) {
                    echo '<li><a href="javascript: void(0);" class="prevnext disablelink">&laquo; First</a></li>';
                }else {
                    echo '<li><a href="/newarticle_1" class="prevnext">&laquo; First</a></li>';
                }

                echo $pages ;
                if($page==$total) {
                    echo '<li><a href="javascript: void(0);" class="prevnext disablelink">Last &raquo;</a></li>';
                }else {
                    echo '<li><a href="/newarticle_'.$total.'" class="prevnext">Last &raquo;</a></li>';
                }
                break;
            case 'cata':
                for ($i=$dau;$i<=$dit;$i++) {
                    if ($i==$page) {
                        $pages.='<li><a href="javascript: void(0);" class="currentpage">'.$i.'</a></li>';
                    }else {
                        $pages.='<li><a href="/cata/'.$link.'/page/'.$i.'">'.$i.'</a></li>';
                    }
                }
                if($page==1) {
                    echo '<li><a href="javascript: void(0);" class="prevnext disablelink">&laquo; First</a></li>';
                }else {
                    echo '<li><a href="/cata/'.$link.'/page/1" class="prevnext">&laquo; First</a></li>';
                }

                echo $pages ;
                if($page==$total) {
                    echo '<li><a href="javascript: void(0);" class="prevnext disablelink">Last &raquo;</a></li>';
                }else {
                    echo '<li><a href="/cata/'.$link.'/page/'.$total.'" class="prevnext">Last &raquo;</a></li>';
                }
                break;
            case 'topread':
                for ($i=$dau;$i<=$dit;$i++) {
                    if ($i==$page) {
                        $pages.='<li><a href="javascript: void(0);" class="currentpage">'.$i.'</a></li>';
                    }else {
                        $pages.='<li><a href="/topread/page/'.$i.'">'.$i.'</a></li>';
                    }
                }
                if($page==1) {
                    echo '<li><a href="javascript: void(0);" class="prevnext disablelink">&laquo; First</a></li>';
                }else {
                    echo '<li><a href="/topread/page/1" class="prevnext">&laquo; First</a></li>';
                }

                echo $pages ;
                if($page==$total) {
                    echo '<li><a href="javascript: void(0);" class="prevnext disablelink">Last &raquo;</a></li>';
                }else {
                    echo '<li><a href="/topread/page/'.$total.'" class="prevnext">Last &raquo;</a></li>';
                }
                break;
            case 'search':
                for ($i=$dau;$i<=$dit;$i++) {
                    if ($i==$page) {
                        $pages.='<li><a href="javascript: void(0);" class="currentpage">'.$i.'</a></li>';
                    }else {
                        $pages.='<li><a href="/search/'.$config['keyword'].'/page/'.$i.'">'.$i.'</a></li>';
                    }
                }
                if($page==1) {
                    echo '<li><a href="javascript: void(0);" class="prevnext disablelink">&laquo; First</a></li>';
                }else {
                    echo '<li><a href="/search/'.$config['keyword'].'/page/1" class="prevnext">&laquo; First</a></li>';
                }

                echo $pages ;
                if($page==$total) {
                    echo '<li><a href="javascript: void(0);" class="prevnext disablelink">Last &raquo;</a></li>';
                }else {
                    echo '<li><a href="/search/'.$config['keyword'].'/page/'.$total.'" class="prevnext">Last &raquo;</a></li>';
                }
                break;
            case 'archives':
                for ($i=$dau;$i<=$dit;$i++) {
                    if ($i==$page) {
                        $pages.='<li><a href="javascript: void(0);" class="currentpage">'.$i.'</a></li>';
                    }else {
                        $pages.='<li><a href="/'.$params[0].'/'.$params[1].'/page/'.$i.'">'.$i.'</a></li>';
                    }
                }
                if($page==1) {
                    echo '<li><a href="javascript: void(0);" class="prevnext disablelink">&laquo; First</a></li>';
                }else {
                    echo '<li><a href="/'.$params[0].'/'.$params[1].'/page/1" class="prevnext">&laquo; First</a></li>';
                }

                echo $pages ;
                if($page==$total) {
                    echo '<li><a href="javascript: void(0);" class="prevnext disablelink">Last &raquo;</a></li>';
                }else {
                    echo '<li><a href="/'.$params[0].'/'.$params[1].'/page/'.$total.'" class="prevnext">Last &raquo;</a></li>';
                }
                break;
            case 'tag':
                for ($i=$dau;$i<=$dit;$i++) {
                    if ($i==$page) {
                        $pages.='<li><a href="javascript: void(0);" class="currentpage">'.$i.'</a></li>';
                    }else {
                        $pages.='<li><a href="/tag/'.$link.'/page/'.$i.'">'.$i.'</a></li>';
                    }
                }
                if($page==1) {
                    echo '<li><a href="javascript: void(0);" class="prevnext disablelink">&laquo; First</a></li>';
                }else {
                    echo '<li><a href="/tag/'.$link.'/page/1" class="prevnext">&laquo; First</a></li>';
                }

                echo $pages ;
                if($page==$total) {
                    echo '<li><a href="javascript: void(0);" class="prevnext disablelink">Last &raquo;</a></li>';
                }else {
                    echo '<li><a href="/tag/'.$link.'/page/'.$total.'" class="prevnext">Last &raquo;</a></li>';
                }
                break;
        }
    }else {
        return false;
    }
}


function pagingAdmin($page,$total,$id='',$name='') {
    global $config;
    if ($total>1) {
        if ($page!=1) {
            $dau=$page-5;
            if ($dau<=0) {$dau=1;$dit=5-$page;}
            $dit=$dit+$page+5;
            if ($dit>$total) {
                $dit=$total;$dau=$total-10;
                if ($dau<=0) {$dau=1;}
            }
        }
        else {
            $dau=1;$dit=10;
            if ($dit>$total) {$dit=$total;}
        }
        if ($page >$total) {$dau=1;$dit=1;}

        switch ($_SESSION['adminActiveMenu']) {
            case 'article':
                if($_GET['searchArticleSubmit']=='Search' AND $_GET['articleActionType']=='searchArticle') {
                    for ($i=$dau;$i<=$dit;$i++) {
                        if ($i==$page) {
                            $pages.='<li><a href="#" class="currentpage">'.$i.'</a></li>';
                        }else {
                            $pages.='<li><a href="/'.$config['admin_section'].'/article/'.$i.'&searchKeyword='.$_GET['searchKeyword'].'&articleActionType=searchArticle&dateFrom='.$_GET['dateFrom'].'&dateTo='.$_GET['dateTo'].'&categoryId='.$_GET['categoryId'].'&searchArticleSubmit=Search">'.$i.'</a></li>';
                        }
                    }
                    if($page==1) {
                        echo '<li><a href="#" class="prevnext disablelink">&laquo; First</a></li>';
                    }else {
                        echo '<li><a href="/'.$config['admin_section'].'/article/1&searchKeyword='.$_GET['searchKeyword'].'&articleActionType=searchArticle&dateFrom='.$_GET['dateFrom'].'&dateTo='.$_GET['dateTo'].'&categoryId='.$_GET['categoryId'].'&searchArticleSubmit=Search" class="prevnext">&laquo; First</a></li>';
                    }

                    echo $pages ;
                    if($page==$total) {
                        echo '<li><a href="#" class="prevnext disablelink">Last &raquo;</a></li>';
                    }else {
                        echo '<li><a href="/'.$config['admin_section'].'/article/'.$total.'&searchKeyword='.$_GET['searchKeyword'].'&articleActionType=searchArticle&dateFrom='.$_GET['dateFrom'].'&dateTo='.$_GET['dateTo'].'&categoryId='.$_GET['categoryId'].'&searchArticleSubmit=Search" class="prevnext">Last &raquo;</a></li>';
                    }
                }else {
                    for ($i=$dau;$i<=$dit;$i++) {
                        if ($i==$page) {
                            $pages.='<li><a href="#" class="currentpage">'.$i.'</a></li>';
                        }else {
                            $pages.='<li><a href="/'.$config['admin_section'].'/article/'.$i.'">'.$i.'</a></li>';
                        }
                    }
                    if($page==1) {
                        echo '<li><a href="#" class="prevnext disablelink">&laquo; First</a></li>';
                    }else {
                        echo '<li><a href="/'.$config['admin_section'].'/article/1" class="prevnext">&laquo; First</a></li>';
                    }

                    echo $pages ;
                    if($page==$total) {
                        echo '<li><a href="#" class="prevnext disablelink">Last &raquo;</a></li>';
                    }else {
                        echo '<li><a href="/'.$config['admin_section'].'/article/'.$total.'" class="prevnext">Last &raquo;</a></li>';
                    }
                }

                break;
            case 'searchlog':
                for ($i=$dau;$i<=$dit;$i++) {
                    if ($i==$page) {
                        $pages.='<li><a href="#" class="currentpage">'.$i.'</a></li>';
                    }else {
                        $pages.='<li><a href="/'.$config['admin_section'].'/searchlog/'.$i.'">'.$i.'</a></li>';
                    }
                }
                if($page==1) {
                    echo '<li><a href="#" class="prevnext disablelink">&laquo; First</a></li>';
                }else {
                    echo '<li><a href="/'.$config['admin_section'].'/searchlog/1" class="prevnext">&laquo; First</a></li>';
                }

                echo $pages ;
                if($page==$total) {
                    echo '<li><a href="#" class="prevnext disablelink">Last &raquo;</a></li>';
                }else {
                    echo '<li><a href="/'.$config['admin_section'].'/searchlog/'.$total.'" class="prevnext">Last &raquo;</a></li>';
                }
                break;
        }
    }else {
        return false;
    }
}

function createThumb($img,$size='137-113') {
    $posMedia = strpos($img, 'media.php');

    if($img=='') {
        if($size=='137-113') {
            return '/uploads/house137113.jpg';
        }elseif($size=='47-47') {
            return '/uploads/house4747.jpg';
        }elseif($size=='107-106') {
            return '/uploads/house107106.jpg';
        }
    }else {
        if($size=='340-239') {//hien thi tren front page
            if($posMedia==FALSE) {
                return '/media.php?src='.str_replace('thumb-137-113/', '', $img).'&zc=1&q=80&w=340&h=239';
            }else {
                return str_replace('w=137&h=113', 'w=340&h=239', $img);
            }
        }elseif($size=='137-113') {
            return $img;
        }elseif($size=='47-47') {
            if($posMedia==FALSE) {
                return str_replace('thumb-137-113', 'thumb-47-47', $img);
            }else {
                return str_replace('w=137&h=113', 'w=47&h=47', $img);
            }
        }elseif($size=='107-106') {
            if($posMedia==FALSE) {
                return str_replace('thumb-137-113', 'thumb-107-106', $img);
            }else {
                return str_replace('w=137&h=113', 'w=107&h=106', $img);
            }
        }
    }
}
function viewTitleLink($string) {
    return htmlentities($string,ENT_COMPAT,'UTF-8');
}
function checkPermission() {
    global $DB;
    $username = $_SESSION['username'];
    $sql = "SELECT deny_modules FROM permission WHERE username='$username'";
    $rsModules = $DB->query($sql);
    while($rs=$DB->fetch_array($rsModules)) {
        $arrDenyModules[] = $rs['deny_modules'];
    }
    if (in_array($_SESSION['adminActiveMenu'], $arrDenyModules)) {
        echo 'Sory, bạn không có quyền truy cập trang này, Click vào <INPUT TYPE="BUTTON" VALUE=" đây " ONCLICK="history.go(-1);"> để quay lại trang trước!';die;
    }
}
/*
function createThumb($img,$strFind='',$strReplace='') {
    $pos = strpos($img, 'media.php');
    if($img!='') {
        if($strReplace!='') {
            return str_replace($strFind, $strReplace, $img);
        }else {
            return $img;
        }
    }else {
        if($strReplace!='') {
            return str_replace('w=137&h=113', $strReplace, '/media.php?src=/uploads/house.jpg&zc=1&q=80&w=137&h=113');
        }else {
            return '/media.php?src=/uploads/house.jpg&zc=1&q=80&w=137&h=113';
        }
    }
}*/
function print_gzipped_page() {

    global $HTTP_ACCEPT_ENCODING;
    if( headers_sent() ){
        $encoding = false;
    }elseif( strpos($HTTP_ACCEPT_ENCODING, 'x-gzip') !== false ){
        $encoding = 'x-gzip';
    }elseif( strpos($HTTP_ACCEPT_ENCODING,'gzip') !== false ){
        $encoding = 'gzip';
    }else{
        $encoding = false;
    }

    if( $encoding ){
        $contents = ob_get_contents();
        ob_end_clean();
        header('Content-Encoding: '.$encoding);
        print("\x1f\x8b\x08\x00\x00\x00\x00\x00");
        $size = strlen($contents);
        $contents = gzcompress($contents, 9);
        $contents = substr($contents, 0, $size);
        print($contents);
        exit();
    }else{
        ob_end_flush();
        exit();
    }
}
function echo_memory_usage() {
    $mem_usage = memory_get_usage(true);
    if ($mem_usage < 1024)
        echo $mem_usage." bytes";
    elseif ($mem_usage < 1048576)
        echo round($mem_usage/1024,2)." kilobytes";
    else
        echo round($mem_usage/1048576,2)." megabytes";

    echo "<br/>";
}

function remove_invisible_characters($str)
{
	static $non_displayables;

	if ( ! isset($non_displayables))
	{
		// every control character except newline (dec 10), carriage return (dec 13), and horizontal tab (dec 09),
		$non_displayables = array(
				'/%0[0-8bcef]/',			// url encoded 00-08, 11, 12, 14, 15
				'/%1[0-9a-f]/',				// url encoded 16-31
				'/[\x00-\x08]/',			// 00-08
				'/\x0b/', '/\x0c/',			// 11, 12
				'/[\x0e-\x1f]/'				// 14-31
		);
	}

	do
	{
		$cleaned = $str;
		$str = preg_replace($non_displayables, '', $str);
	}
	while ($cleaned != $str);

	return $str;
}
?>